<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Http\Admin\Logistics\Controllers;

use App\Eloquent\Province;
use App\Eloquent\Ygt\LogisticsLine;
use Framework\BaseClass\Http\Admin\Controller;

class LineController extends Controller
{
    public function index(){}
    public function lists()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $logisticsId        = request('logistics_id',0);
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = ['company_id'=>$companyId,'logistics_id'=>$logisticsId];
            $lists          = LogisticsLine::getList($where,'',$pageSize,$offset)->toArray();
            foreach($lists as $key=>$val)
            {
                $contactAddress     = $val['province_name'].$val['city_name'].$val['area_name'].$val['address'];
                $lists[$key]['contact_address'] = $contactAddress;
            }
            $count          = LogisticsLine::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $result         = json_encode($data);
            return $result;
        } else {
            return $this->view('lists',compact('logisticsId'));
        }
    }
    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $isSubmit           = request('is_submit',0);
        $lineId             = request('id',0);
        $logisticsId        = request('logistics_id',0);

        if($isSubmit){
            $data                   = request()->all();
            $data['company_id']   = $companyId;
            if($lineId==0)
            {
                $data['logistics_id']   = $logisticsId;
            }
            $fieldList              = LogisticsLine::$tableFieldList;
            $tableData              = LogisticsLine::setDataByTableField($data,$fieldList);
            $tableDataCheckResult   = LogisticsLine::checkDataByTableField($tableData,$fieldList);
            if($tableDataCheckResult !== true)
            {
                return $this->ajaxFail( $tableDataCheckResult );
            }
            $editResult             = LogisticsLine::edit($tableData);
            if( $editResult ){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail( '失败' );
            }
        } else {
            if($lineId){
                $where              = ['id'=>$lineId];
                $info               = LogisticsLine::getInfo($where);
            }else{
                $info               = LogisticsLine::getObject();
            }
            $provinceList           = Province::get()->toArray();
            return $this->view('edit',compact('info','logisticsId','provinceList'));
        }
    }
    public function delete(){}
}